clear
use Data/Pew_Models.dta, clear

recode field 1=0 2=1, into(foreign)


*Testing for trends
sort survey
egen tag = tag(survey)

eststo unitTrend: reg unit year if tag==1
scalar unitB = `:di %6.2g _b[year]'
scalar unitC = `:di %6.2g _b[_cons]'
scalar unitR2 = `:di %6.2g e(r2)' 


foreach x in economy civil_rights energy immigration welfare foreign {
eststo `x'Trend: reg abd_d year if `x'==1
scalar `x'B = `:di %6.2g _b[year]'
scalar `x'C = `:di %6.2g _b[_cons]'
scalar `x'R2 = `:di %6.2g e(r2)' 

if (`x'C<0) {
scalar `x'Sign = "-"
}
else if (`x'C>=0) {
scalar `x'Sign = "+"
}


}

esttab unitTrend economyTrend energyTrend immigrationTrend civil_rightsTrend welfareTrend foreignTrend using Tables/trend_table.csv, se star(* 0.05 ** 0.01 *** 0.001) replace r2


*Plotting trends*

keep year unit
rename unit y
gen topic = 1
save Working/temp1.dta, replace

use Data/Pew_Models.dta, clear
keep if economy==1
keep id year abd_d
rename abd_d y
gen topic = 2
save Working/temp2.dta, replace

use Data/Pew_Models.dta, clear
keep if energy==1
keep id year abd_d
rename abd_d y
gen topic = 3
save Working/temp4.dta, replace

use Data/Pew_Models.dta, clear
keep if immigration==1
keep id year abd_d
rename abd_d y
gen topic = 4
save Working/temp5.dta, replace

use Data/Pew_Models.dta, clear
keep if civil_rights==1
keep id year abd_d
rename abd_d y
gen topic = 5
save Working/temp3.dta, replace

use Data/Pew_Models.dta, clear
keep if welfare==1
keep id year abd_d
rename abd_d y
gen topic = 6
save Working/temp6.dta, replace

use Data/Pew_Models.dta, clear
keep if field==2
keep id year abd_d
rename abd_d y
gen topic = 7
save Working/temp7.dta, replace

use Working/temp1.dta, clear
forvalues x = 2/7 {
append using Working/temp`x'.dta
}

label define topic ///
1 "Response rate" ///
2 "Economy" ///
3 "Energy" ///
4 "Immigration" ///
5 "Civil rights" ///
6 "Welfare" ///
7 "Foreign policy" ///
, replace


label values topic topic

set scheme s1mono


twoway ///
(scatter y year, mcolor(gs12%10) msize(small) mlcolor(gs12%75) mlwidth(vthin)) ///
(lfit y year, lcolor(black) lpattern(solid)) ///
if topic==1, ///
text(29 2014 "y = `=unitB'*Year + `=unitC'", place(w) size(small)) ///
text(26 2014 "R{superscript:2} = `=unitR2'", place(w) size(small)) ///
xtitle("") xlabel(#11, labsize(vsmall) alternate) ylabel(,labsize(vsmall) angle(horizontal)) xmtick(2005(2)2013, ticks tlength(medium))  plotregion(lcolor(none)) ///
xsize(16) ysize(12) yscale(noextend) xscale(noextend) legend(off) title(Response rate) ytitle(%)

graph save Working/unitTrend.gph, replace

twoway ///
(scatter y year, mcolor(gs12%10) msize(small) mlcolor(gs12%75) mlwidth(vthin)) ///
(lfit y year, lcolor(black) lpattern(solid)) ///
if topic==2, ///
text(1.8 2014 "y = `=economyB'*Year `=economySign' `=abs(economyC)'", place(w) size(small)) ///
text(1.6 2014 "R{superscript:2} = `=economyR2'", place(w) size(small)) ///
xtitle("") xlabel(#11, labsize(vsmall) alternate) ylabel(0 0.5 1 1.5 2,labsize(vsmall) angle(horizontal)) xmtick(2005(2)2013, ticks tlength(medium))  plotregion(lcolor(none)) ///
xsize(16) ysize(12) yscale(noextend) xscale(noextend) legend(off) title(Economy) ytitle(Polarization (Cohen's D))

graph save Working/economyTrend.gph, replace

twoway ///
(scatter y year, mcolor(gs12%10) msize(small) mlcolor(gs12%75) mlwidth(vthin)) ///
(lfit y year, lcolor(black) lpattern(solid)) ///
if topic==3, ///
text(1.8 2014 "y = `=energyB'*Year `=energySign' `=abs(energyC)'", place(w) size(small) ) ///
text(1.6 2014 "R{superscript:2} = `=energyR2'", place(w) size(small)) ///
xtitle("") xlabel(#11, labsize(vsmall) alternate) ylabel(0 0.5 1 1.5 2,labsize(vsmall) angle(horizontal)) xmtick(2005(2)2013, ticks tlength(medium))  plotregion(lcolor(none)) ///
xsize(16) ysize(12) yscale(noextend) xscale(noextend) legend(off) title(Energy) ytitle(Polarization (Cohen's D))

graph save Working/energyTrend.gph, replace

twoway ///
(scatter y year, mcolor(gs12%10) msize(small) mlcolor(gs12%75) mlwidth(vthin)) ///
(lfit y year, lcolor(black) lpattern(solid)) ///
if topic==4, ///
text(1.8 2014 "y = `=immigrationB'*Year `=immigrationSign' `=abs(immigrationC)'", place(w) size(small)) ///
text(1.6 2014 "R{superscript:2} = `=immigrationR2'", place(w) size(small)) ///
xtitle("") xlabel(#11, labsize(vsmall) alternate) ylabel(0 0.5 1 1.5 2,labsize(vsmall) angle(horizontal)) xmtick(2005(2)2013, ticks tlength(medium))  plotregion(lcolor(none)) ///
xsize(16) ysize(12) yscale(noextend) xscale(noextend) legend(off) title(Immigration) ytitle(Polarization (Cohen's D))

graph save Working/immigrationTrend.gph, replace

twoway ///
(scatter y year, mcolor(gs12%10) msize(small) mlcolor(gs12%75) mlwidth(vthin)) ///
(lfit y year, lcolor(black) lpattern(solid)) ///
if topic==5, ///
text(1.8 2014 "y = `=civil_rightsB'*Year `=civil_rightsSign' `=abs(civil_rightsC)'", place(w) size(small)) ///
text(1.6 2014 "R{superscript:2} = `=civil_rightsR2'", place(w) size(small)) ///
xtitle("") xlabel(#11, labsize(vsmall) alternate) ylabel(0 0.5 1 1.5 2,labsize(vsmall) angle(horizontal)) xmtick(2005(2)2013, ticks tlength(medium))  plotregion(lcolor(none)) ///
xsize(16) ysize(12) yscale(noextend) xscale(noextend) legend(off) title(Civil rights) ytitle(Polarization (Cohen's D))

graph save Working/civil_rightsTrend.gph, replace

twoway ///
(scatter y year, mcolor(gs12%10) msize(small) mlcolor(gs12%75) mlwidth(vthin)) ///
(lfit y year, lcolor(black) lpattern(solid)) ///
if topic==6, ///
text(1.8 2014 "y = `=welfareB'*Year `=welfareSign' `=abs(welfareC)'", place(w) size(small)) ///
text(1.6 2014 "R{superscript:2} = `=welfareR2'", place(w) size(small)) ///
xtitle("") xlabel(#11, labsize(vsmall) alternate) ylabel(0 0.5 1 1.5 2,labsize(vsmall) angle(horizontal)) xmtick(2005(2)2013, ticks tlength(medium))  plotregion(lcolor(none)) ///
xsize(16) ysize(12) yscale(noextend) xscale(noextend) legend(off) title(Welfare) ytitle(Polarization (Cohen's D))

graph save Working/welfareTrend.gph, replace

twoway ///
(scatter y year, mcolor(gs12%10) msize(small) mlcolor(gs12%75) mlwidth(vthin)) ///
(lfit y year, lcolor(black) lpattern(solid)) ///
if topic==7, ///
text(1.8 2014 "y = `=foreignB'*Year `=foreignSign' `=abs(foreignC)'", place(w) size(small)) ///
text(1.6 2014 "R{superscript:2} = `=foreignR2'", place(w) size(small)) ///
xtitle("") xlabel(#11, labsize(vsmall) alternate) ylabel(0 0.5 1 1.5 2,labsize(vsmall) angle(horizontal)) xmtick(2005(2)2013, ticks tlength(medium))  plotregion(lcolor(none)) ///
xsize(16) ysize(12) yscale(noextend) xscale(noextend) legend(off) title(Foreign policy) ytitle(Polarization (Cohen's D))

graph save Working/foreignTrend.gph, replace


graph combine ///
Working/unitTrend.gph ///
Working/economyTrend.gph ///
Working/energyTrend.gph ///
Working/immigrationTrend.gph ///
Working/civil_rightsTrend.gph ///
Working/welfareTrend.gph ///
Working/foreignTrend.gph ///
, xcommon rows(3) imargin(zero) xsize(16) ysize(11) graphregion(margin(zero))  holes(1 3)


gr_edit .plotregion1.graph1.title.style.editstyle size(medium) editcopy
gr_edit .plotregion1.graph2.title.style.editstyle size(medium) editcopy
gr_edit .plotregion1.graph3.title.style.editstyle size(medium) editcopy
gr_edit .plotregion1.graph4.title.style.editstyle size(medium) editcopy
gr_edit .plotregion1.graph5.title.style.editstyle size(medium) editcopy
gr_edit .plotregion1.graph6.title.style.editstyle size(medium) editcopy
gr_edit .plotregion1.graph7.title.style.editstyle size(medium) editcopy
gr_edit .plotregion1.graph4.yaxis1.title.draw_view.setstyle, style(no)
gr_edit .plotregion1.graph7.yaxis1.title.draw_view.setstyle, style(no)
gr_edit .plotregion1.graph3.yaxis1.title.draw_view.setstyle, style(no)
gr_edit .plotregion1.graph6.yaxis1.title.draw_view.setstyle, style(no)
gr_edit .plotregion1.graph5.yaxis1.title.draw_view.setstyle, style(no)
gr_edit .plotregion1.graph2.yaxis1.title.draw_view.setstyle, style(no)
gr_edit .l1title.text = {}
gr_edit .l1title.text.Arrpush `"Polarization (Cohen's D)"'
gr_edit .l1title.style.editstyle size(small) editcopy
gr_edit .l1title.DragBy -14.77885652642933 0

graph export Figures/Figure_1.pdf, as(pdf) replace

bysort topic year: egen mean = mean(y)
egen tag = tag(topic year)

keep if tag==1

keep year mean topic
reshape wide mean, i(year) j(topic)

rename mean1 unit
rename mean2 economy
rename mean3 energy
rename mean4 immigration
rename mean5 civil_rights
rename mean6 welfare
rename mean7 foreign

tsset year, yearly

dfuller unit
dfuller economy 
dfuller energy
dfuller immigration
dfuller civil_rights 
dfuller welfare 
dfuller foreign

pperron unit
pperron economy 
pperron energy
pperron immigration
pperron civil_rights 
pperron welfare 
pperron foreign


*Replication
use Data/Pew_Models.dta, clear

recode field 1=0 2=1, into(foreign)

bysort year: egen meanUnit = mean(unit)

foreach x in economy civil_rights energy immigration welfare foreign {

quietly{ 
eststo `x'1: reg abd_d unit d_congress if `x'==1
eststo `x'2: reg abd_d unit d_congress year if `x'==1
eststo `x'3: semipar abd_d unit d_congress if `x'==1, nonpar(year) nograph 
eststo `x'4: reg abd_d unit d_congress i.year if `x'==1
eststo `x'5: mixed abd_d unit d_congress meanUnit if `x'==1 || year:
}
esttab `x'1 `x'2 `x'3 `x'4 `x'5, se star(* 0.05 ** 0.01 *** 0.001)

}

esttab economy1 energy1 immigration1 civil_rights1 welfare1 foreign1 using Tables/table_1.csv, se star(* 0.05 ** 0.01 *** 0.001) keep(unit) replace

forvalues x = 2/5 {
esttab economy`x' energy`x' immigration`x' civil_rights`x' welfare`x' foreign`x' using Tables/table_1.csv, se star(* 0.05 ** 0.01 *** 0.001) keep(unit) append
}


